<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>09 分页</title>
    <script src="./js/vue.js"></script>
    <script src="./js/axios.min.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }
        input{
            outline: none;
        }

        #app {
            margin: 0 auto;
        }

        .table {
            margin: 20px auto;
            width: 1200px;
        }

        .table .item {
            margin-right: 40px;
            width: 200px;
            height: 300px;
            float: left;
        }

        .table .item .img {
            width: 200px;
            height: 200px;
        }

        .type {
            width: 1200px;
            margin: 20px auto;
        }
        .sort {
            width: 1200px;
            margin: 20px auto;
        }
        .search {
            width: 1200px;
            margin: 20px auto;
        }  
        .scope {
            width: 1200px;
            margin: 20px auto;
        } 
        .page {
            width: 1200px;
            height: 40px;
            line-height: 40px;
            margin: 20px auto;
            text-align: center;
            display: flex;
            justify-content: center;
        } 
        .page .item{
            width: 40px;
            height: 40px;
            color: white;
            margin: 0 5px;
            background-color: darkblue;
        }
        .page .active{
            background-color: blue;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="search">
            搜索
            <input type="text" v-model="keywords">
        </div>
        <div class="scope">
            价格区间：
            从<input type="number" v-model="start"> 到 <input type="number" v-model="end">之间
        </div>
        <div class="sort">
            <p>
                排序字段
                ID <input type="radio" name="sort" value="id" v-model="sort">
                价格 <input type="radio" name="sort" value="goodsprice" v-model="sort">
            </p>
            <p>
                排序方式
                升序 <input type="radio" name="order" value="asc" v-model="order">
                降序 <input type="radio" name="order" value="desc" v-model="order">
            </p>
        </div>
        <div class="type">
            <p>
                分类
                全部<input type="radio" name="type" value="" v-model="type">
                服装<input type="radio" name="type" value="1" v-model="type">
                手机<input type="radio" name="type" value="2" v-model="type">
                箱包<input type="radio" name="type" value="3" v-model="type">
                美妆<input type="radio" name="type" value="4" v-model="type">
            </p>
        </div>
        <div class="page">
            <button v-if="page > 1" @click="prev">上一页</button>
            <button v-else disabled>上一页</button>
            <div :class="{active:item == page}" class="item" v-for="item in pagelist" >
                {{item}}
            </div>
            
            <button v-if="page < maxpage" @click="next">下一页</button>
            <button v-else  disabled>下一页</button>
        </div>
        <div class="table">
            <div class="item" v-for="item in list" :key="item.id">
                <img :src="item.goodsimg" alt="" class="img">
                <p>商品名称：{{item.goodsname}}</p>
                <p class="num">数量{{item.goodsnum}}</p>
                <p class="price">价格{{item.goodsprice}}</p>
            </div>
        </div>

    </div>

</body>
<script>
    new Vue({
        el: "#app",
        data: {
            list: [],
            type: "",
            sort:"id",
            order:"asc",
            keywords:"",
            timer:null,
            start:"",
            end:"",
            activeIndex:1,

            // 分页

            page:1,
            limit:5,
            maxpage:20,
            total:200,
            showpage:5,
            pagelist:[],

        },
        async created() {

            this.search();
        },
        methods: {
            prev(){
                this.page--;
            },
            next(){
                this.page++;
            },

            async search() {

                let params = {

                    _sort:this.sort,
                    _order:this.order,
                    q:this.keywords,
                    _limit:this.limit,
                    _page:this.page,
    
                }
                
                if(this.type){
                    params.type = this.type;
                }

                // 起始价格
                if(this.start){
                    params.goodsprice_gte = this.start;
                }
                //截止价格
                if(this.end){
                    params.goodsprice_lte = this.end;
                }

                // 分页


                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2345/goodslist",
                    params,
                })

                this.list = list;

                // 获取分页的总条数
                delete params._limit;
                delete params._page;
                let {data:list2} = await axios({
                    method: "get",
                    url: "http://localhost:2345/goodslist",
                    params,
                })
                
                // 计算总条数   和  最大页码
                this.total = list2.length;
                this.maxpage = Math.ceil( this.total / this.limit );
                // 计算中间数
                let middlenum = Math.ceil(this.showpage / 2);

                // 数组清空
                this.pagelist.length = 0;
                
                let start = 1;     //页码开头
                let end = this.maxpage > this.showpage ? this.showpage : this.maxpage;  //页码结尾

                console.log(end)
                // 页码在中间时候
                if(this.page > middlenum){
                    start = this.page - middlenum + 1;
                    end = this.page +middlenum - 1;
                }

                // 页码在结尾的时候
                if(this.page > this.maxpage - middlenum){
                    start = this.maxpage - this.showpage + 1;
                    end = this.maxpage;
                }


                start = start > 1 ? start: 1;
                for(let i = start; i<=end; i++){
                    this.pagelist.push(i);
                }
            }


        },
        watch: {
            // 分页
            page(){
                this.search();
            },
            type(){
                this.page = 1;
                this.search();
            },
            // 排序字段
            sort(){
                this.search();
            },
            // 排序类型
            order(){
                this.search();
            },
            // 关键词搜索
            keywords(){
                this.page = 1;
                // 防抖
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    this.search();
                },1500)
            },
            start(){
                this.page = 1;
                // 防抖
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    this.search();
                },1500);
            },
            end(){
                this.page = 1;
                // 防抖
                clearTimeout(this.timer);
                this.timer = setTimeout(() => {
                    this.search();
                },1500);
            },

        }
    })
</script>

</html>